import MainMapLogViewUI = layaUI.main.MainMapLogViewUI;

class MainMapLogView extends MainMapLogViewUI{

    private _model:MapModel;//数据
    private _nowPage:number;//当前页
    private _maxPage:number;//最大页
    private static _reqCount:number;//请求的最大纪元
    private _historyList:Array<LogVo>;//历史记录
    private _masterHistoryList:Array<LogVo>;//自己的历史记录
    public isShowMaster:boolean;//是否是显示自己胜率的

    constructor() {
        super();
        //数据
        this._model = MapModel.getInstance();
        //list render
        this.list.renderHandler = new Handler(this,this.onRender);
        //描述
        this.txtArea.style.align = "center";
        this.txtArea.style.leading = 10;
        //默认隐藏
        this.btnPrev.visible = false;
        this.tabPage.visible = false;
        this.boxNext.visible = false;
        //添加侦听
        this.tabPage.on(Laya.Event.CHANGE,this,this.itemChangeHandler);
        this.btnPrev.on(Laya.Event.CLICK,this,this.prevClickHandler);
        this.btnNext.on(Laya.Event.CLICK,this,this.nextClickHandler);
        this.btnLast.on(Laya.Event.CLICK,this,this.lastClickHandler);
        //按钮文字
        this.btnPrev.label = LangUtils.getText(28);
        this.btnNext.label = LangUtils.getText(29);
        this.btnLast.label = LangUtils.getText(30);
        //语言
        LangUtils.setSkin(this.img1);
        //cdn加载
        ResUtils.setLoadSkin(this.imgBig1);
        ResUtils.setLoadSkin(this.imgBig2);
    }

    /**
     * 添加侦听
     */
    public addEvent():void{
        //当前有纪元信息 请求刷最新的记录
        if(this._model.nowCount != 0){
            //请求刷最新的记录
            this.reqNewHis();
        }
        //没有纪元信息 等待纪元信息刷新
        else{
            //显示遮罩
            this.mouseEnabled = false;
            MainMaskView.getInstance().open();
            this._model.on(MapModel.INFO_CHANGE,this,this.infoChangeHandler);
        }
        this._model.on(MapModel.HISTORY_INFO_CHANGE,this,this.historyChangeHandler);
    }

    /**
     * 移除侦听
     */
    public removeEvent():void{
        this._model.off(MapModel.INFO_CHANGE,this,this.infoChangeHandler);
        this._model.off(MapModel.HISTORY_INFO_CHANGE,this,this.historyChangeHandler);
        MainMaskView.getInstance().close();
    }

    //纪元信息变更
    private infoChangeHandler():void{
        //当前更新的不是纪元信息 不刷新
        if(this._model.nowCount == 0){
            return;
        }
        //请求刷最新的记录
        this.reqNewHis();
        //移除侦听
        this._model.off(MapModel.INFO_CHANGE,this,this.infoChangeHandler);
    }

    //请求刷最新的记录
    private reqNewHis():void{
        //刷新纪元信息
        this.updateCount();
        //重置第一页
        this._nowPage = 1;
        //检测历史是否请求一致
        if(MainMapLogView._reqCount == this._model.nowCount - 1 && this._model.historyList != null){
            //刷新历史记录分页
            this.updatePage(this._nowPage);
            return;
        }
        //显示遮罩
        MainMaskView.getInstance().open();
        MainMapLogView._reqCount = this._model.nowCount - 1;
        //清空自己的记录
        this._masterHistoryList = null;
        //请求数据
        this.list.array = [];
        this._model.reqHistory(1,MainMapLogView._reqCount);
    }

    //历史信息变更
    private historyChangeHandler():void{
        this.mouseEnabled = true;
        //刷新历史记录分页
        this.updatePage(this._nowPage);
        //移除遮罩
        MainMaskView.getInstance().close();
    }

    //刷新历史记录分页
    private updatePage(page:number):void{
        //检测是否是显示自己的
        this._historyList = this.isShowMaster == true ? this.getMasterHistoryList() : this._model.historyList;
        this._maxPage = Math.ceil(this._historyList.length / 10);
        this._nowPage = page;
        //分页
        var startIndex:number = (page - 1) * 10;
        var endIndex:number = startIndex + 10;
        var maxIndex:number = this._historyList.length;
        endIndex = endIndex > maxIndex ? maxIndex : endIndex;
        this.list.array = this._historyList.slice(startIndex,endIndex);
        //分页
        var nowPage:number = page;
        var pageNumList:Array<number> = [nowPage];
        var minPage:number;
        var maxPage:number;
        for(var i:number=1;i<=5;i++){
            minPage = nowPage - i;
            maxPage = nowPage + i;
            if(minPage > 0){
                pageNumList.push(minPage);
            }
            if(maxPage <= this._maxPage && maxPage > 0){
                pageNumList.push(maxPage);
            }
            if(pageNumList.length >= 5){
                break;
            }
        }
        pageNumList = pageNumList.sort();
        //检查页数
        if(pageNumList.length == 1){
            return;
        }
        this.btnPrev.visible = true;
        this.tabPage.visible = true;
        this.boxNext.visible = true;
        this.tabPage.labels = pageNumList.toString();
        //布局
        var tabWidth:number = this.tabPage.width * 0.8;
        this.tabPage.x = (this.width - tabWidth) / 2;
        this.boxNext.x = this.tabPage.x + tabWidth + 20;
        this.btnPrev.x = this.tabPage.x - this.btnPrev.width * 0.8 - 20;
        //选中
        this.tabPage.selectedIndex = pageNumList.indexOf(nowPage);
        this.btnPrev.visible = nowPage != 1;
        this.boxNext.visible = nowPage != this._maxPage;
    }

    //list render
    private onRender(cell:MapLogItem,index:number):void{
        cell.setData(cell.dataSource,index,this.isShowMaster);
        //布局居中
        if(index == 0){
            this.list.x = (this.width - cell.txtDesc.contextWidth) / 2;
        }
    }

    //刷新纪元信息
    private updateCount():void{
        if(this.isShowMaster == false){
            var content:string = LangUtils.getText(7);
            this.txtArea.innerHTML = content.replace("[1]",(this._model.nowCount-1)+"");
        }else{
            this.txtArea.innerHTML = LangUtils.getText(23);
        }
    }

    //翻页改变
    private itemChangeHandler():void{
        var page:number = this.tabPage.selection["label"];
        if(page != this._nowPage){
            this.updatePage(Number(page));
        }
    }

    //上一页
    private prevClickHandler():void{
        var page:number = this._nowPage - 1;
        if(page <= 0){
            return;
        }
        this.updatePage(page);
    }

    //下一页
    private nextClickHandler():void{
        var page:number = this._nowPage + 1;
        if(page > this._maxPage){
            return;
        }
        this.updatePage(page);
    }

    //最后一页
    private lastClickHandler():void{
        this.updatePage(this._maxPage);
    }

    //请求记录
    private reqHistory(page:number):void{
        this._model.reqHistory((page - 1) * 10 + 1,10);
        this.mouseEnabled = false;
        MainMaskView.getInstance().open();
    }

    //获取自己的历史记录
    private getMasterHistoryList():Array<LogVo>{
        if(this._masterHistoryList == null){
            this._masterHistoryList = [];
            var logVo:LogVo;
            for(var i:number=0;i<this._model.historyList.length;i++){
                logVo = this._model.historyList[i];
                if(logVo.address == Web3Manager.defaultAccount){
                    this._masterHistoryList.push(logVo);
                }
            }
        }
        return this._masterHistoryList;
    }
}